/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.item.etcitem.action;

import com.lineage.server.PacketCreate;
import com.lineage.server.model.L1ItemDelay;
import com.lineage.server.model.L1Location;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.model.L1World;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.item.ItemExecutor;
import com.lineage.server.packetserver.S_Paralysis;
import com.lineage.server.packetserver.S_ServerMessage;
import com.lineage.server.templates.L1BookMark;

/**
 * Class <code>TeleportsScroll</code> 瞬间移动类卷轴.<br>
 * 40100 - 瞬间移动卷轴 <br>
 * 40086 - 全体传送术的卷轴 <br>
 * 40099 - 象牙塔瞬间移动卷轴 <br>
 * 40863 - 魔法卷轴 (指定传送) <br>
 * 140100 - 受祝福的 瞬间移动卷轴 <br>
 * 
 * @author jrwz
 * @version 2012-6-19上午11:01:16
 * @see com.lineage.server.model.item.etcitem.action
 * @since JDK1.7
 */
public final class TeleportsScroll extends ItemExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static ItemExecutor get() {
        return new TeleportsScroll();
    }

    /** 瞬间移动类卷轴. */
    private TeleportsScroll() {
    }

    @Override
    public void execute(final L1PcInstance pc, final L1ItemInstance useItem,
            final int readC1, final int readC2, final int targetId,
            final int targetX, final int targetY) {
        final int btele = targetId;
        final L1BookMark bookm = pc.getBookMark(btele);

        if (bookm != null) { // 取得坐标点
            if (pc.getMap().isEscapable() || pc.isGm()) {
                final int newX = bookm.getLocX();
                final int newY = bookm.getLocY();
                final int mapId = bookm.getMapId();
                final int itemId = useItem.getItemId();

                // 全体传送术的卷轴
                if (itemId == 40086) {
                    for (final L1PcInstance member : L1World.getInstance()
                            .getVisiblePlayer(pc)) {
                        if ((pc.getLocation().getTileLineDistance(
                                member.getLocation()) <= 3)
                                && (member.getClanid() == pc.getClanid())
                                && (pc.getClanid() != 0)
                                && (member.getId() != pc.getId())) {
                            L1Teleport.teleport(member, newX, newY, mapId, 5,
                                    true);
                        }
                    }
                }

                L1Teleport.teleport(pc, newX, newY, mapId, 5, true);
                L1ItemDelay.teleportUnlock(pc, useItem); // 卷轴传送后 使用物品延迟完才解开停止状态
                pc.getInventory().removeItem(useItem, 1);
            } else {
                PacketCreate.get().sendSinglePackets(pc, "没有任何事情发生");
                pc.sendPackets(new S_Paralysis(
                        S_Paralysis.TYPE_TELEPORT_UNLOCK, true));
            }
        } else {
            if (pc.getMap().isTeleportable() || pc.isGm()) {
                final L1Location newLocation = pc.getLocation().randomLocation(
                        200, true);
                final int newX = newLocation.getX();
                final int newY = newLocation.getY();
                final int mapId = newLocation.getMapId();
                final int itemId = useItem.getItemId();

                // 全体传送术的卷轴
                if (itemId == 40086) {
                    for (final L1PcInstance member : L1World.getInstance()
                            .getVisiblePlayer(pc)) {
                        if ((pc.getLocation().getTileLineDistance(
                                member.getLocation()) <= 3)
                                && (member.getClanid() == pc.getClanid())
                                && (pc.getClanid() != 0)
                                && (member.getId() != pc.getId())) {
                            L1Teleport.teleport(member, newX, newY, mapId, 5,
                                    true);
                        }
                    }
                }
                L1Teleport.teleport(pc, newX, newY, mapId, 5, true);
                L1ItemDelay.teleportUnlock(pc, useItem); // 卷轴传送后 使用物品延迟完才解开停止状态
                pc.getInventory().removeItem(useItem, 1);
            } else {
                pc.sendPackets(new S_ServerMessage(276));
                pc.sendPackets(new S_Paralysis(
                        S_Paralysis.TYPE_TELEPORT_UNLOCK, true));
            }
        }
    }
}
